package com.vst.pushlib;

import java.util.HashMap;
import java.util.Map;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

@SuppressLint("UseSparseArrays")
public class PushConstant {
    public static final String TAG = "VstPush";

    public static final String RESPONSE_METHOD = "method";

    public static final String RESPONSE_CONTENT = "content";

    public static final String EXTRA_MESSAGE = "message";

    public static final String EXTRA_KEY = "push_key";

    public static final String EXTRA_STATE = "push_state";

    public static final String EXTRA_POS = "push_pos";

    public static final String EXTRA_VOLUME = "push_volume";

    public static final String EXTRA_MUTE = "push_mute";

    public static final String EXTRA_DURATION = "push_duration";

    public static final String EXTRA_URL = "push_url";

    public static final String EXTRA_API_ID = "appid";

    public static final String EXTRA_CHANNEL_ID = "channel_id";

    public static final String EXTRA_USER_ID = "user_id";

    public static final String EXTRA_TITLE = "push_title";

    public static final String EXTRA_LIVE = "push_live";

    public static final String EXTRA_VID = "push_vid";

    static final String EXTRA_WEIXIN = "key_weixin";

    static final String EXTRA_BIND_FLAG = "bind_flag";

    static final String SP_NAME = "push_sp";

    // ACTION
    public static final String ACTION_LOGIN_BY_PUSH = "com.vst.action.PUSH.LOGIN";

    public static final String ACTION_LIVEPLAY_BY_PUSH = "com.vst.action.PUSH.LIVEPLAY";

    public static final String ACTION_VIDEOPLAY_BY_PUSH = "com.vst.action.PUSH.VIDEOPLAY";

    public static final String ACTION_TDCODE_BY_PUSH = "com.vst.action.PUSH.TDCODE";

    public static final String ACTION_BINDKEY_BY_PUSH = "com.vst.action.PUSH.BINDKEY";

    public static final String ACTION_DOWNLOADAPK_PUSH = "com.vst.action.PUSH.DOWNLOADAPK";

    public static final String ACTION_COMMENT = "com.vst.action.push_COMMENT";

    private static final String PUSH_ACTION_V1 = "com.vst.play.PUSH_V1";

    private static final String PUSH_ACTION_V2 = "com.vst.play.PUSH_V2";

    private static final String PUSH_ACTION_PHONE = "com.vst.play.PUSH_PHONE";

    private static final String PUSH_ACTION_ALI = "com.vst.play.PUSH_ALI ";

    public static final String PUSH_PKG = "pkg"; // 当前推送应用的包名 用来广播接收区分
    // app version
    public static final int VER_1 = 1;

    public static final int VER_2 = VER_1 + 1;

    public static final int VER_PHONE = VER_2 + 1;

    public static final int VER_LIVE = VER_PHONE + 1;

    public static final int VER_CLOUD_TV = VER_LIVE + 1;

    public static final int VER_4_ALI = VER_CLOUD_TV + 1;

    private static Map<Integer, String> API_KEYS = new HashMap<Integer, String>();

    private static Map<Integer, String> SECRET_KEYS = new HashMap<Integer, String>();

    static {
        API_KEYS.put(VER_PHONE, "Dz61x6GwFxGmoijFWXLU1ggz");
        API_KEYS.put(VER_1, "xjub159AsxEQf6enjhInZacN");
        API_KEYS.put(VER_2, "XsfOsnOunKnya3zVrZ4nY6wN");
        API_KEYS.put(VER_LIVE, "z7roPcYjm9BXotblkKE2KCX3");
        API_KEYS.put(VER_CLOUD_TV, "qsMCnkVwNnrUmUaXe3mIQrRr");
        API_KEYS.put(VER_4_ALI, "a33ats8Uo0C74b8T9BNZm1d5");

        SECRET_KEYS.put(VER_PHONE, "P6PiZtLbnmL2LAi4TySiQ9lKwS8StBV2");
        SECRET_KEYS.put(VER_1, "iMXx2Y8MkBP962T9ecOZyGEgW4xedLfz");
        SECRET_KEYS.put(VER_2, "sm66hiW7peD1QpziLd0tRQL4eTrdLDYN");
        SECRET_KEYS.put(VER_LIVE, "x6DA161v9aGUzmVnQCi1roC7YGQ31XrU");
        SECRET_KEYS.put(VER_CLOUD_TV, "ASMTQ31AeFP048egVYCTisTRMZ8jGx4e");
        SECRET_KEYS.put(VER_4_ALI, "7rr2THWgMn7ABYIjqoGZV1096D93tMwT");
    }

    private static String BIND_KEY;

    public static final int STATE_IDLE = 0;

    public static final int STATE_PREPARING = STATE_IDLE + 1;

    public static final int STATE_PREPARED = STATE_PREPARING + 1;

    public static final int STATE_PLAY = STATE_PREPARED + 1;

    public static final int STATE_PAUSE = STATE_PLAY + 1;

    public static final int STATE_STOP = STATE_PAUSE + 1;

    public static final int STATE_COMPLETE = STATE_STOP + 1;

    public static final int STATE_ERROR = STATE_IDLE - 1;

    public static final void registerBaiduPush(Context ctx, int version) {

        Log.d(TAG, "isBind=" + getBind(ctx) + "  isPushEnabled=" + PushManager.isPushEnabled(ctx) + " version="
                + sVERSION);

        // if (!getBind(ctx) || !PushManager.isPushEnabled(ctx)) {
        // 已经解绑或者push已经被停止
        PushManager.startWork(ctx.getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, getApiKey(version));
        // }

    }

    public static final void unrigisterBaiduPush(Context ctx) {
        if (PushManager.isPushEnabled(ctx)) {
            PushManager.stopWork(ctx);
        }

    }

    public static final String getApiKey(int version) {
        sVERSION = version;
        try {

            return API_KEYS.get(version);

        } catch (Exception e) {
        }
        return null;
    }

    static final String getApiKey() {
        return getApiKey(sVERSION);
    }

    static final String getSecretKey() {
        try {

            return SECRET_KEYS.get(sVERSION);

        } catch (Exception e) {
        }
        return null;
    }

    static final void setBindKey(String str) {
        BIND_KEY = str;
    }

    static final String getBindKey() {
        return BIND_KEY;
    }

    private static int sVERSION = VER_PHONE;

    public static String getAction() {
        switch (sVERSION) {
        case VER_1:
            return PUSH_ACTION_V1;
        case VER_2:
            return PUSH_ACTION_V2;
        default:
            return PUSH_ACTION_PHONE;
        }
    }

    static String getControlAction() {
        return "com.vst.push.control.action" + PushConstant.getVersion();
    }

    static int getVersion() {
        return sVERSION;
    }

    public static String getString(Context ctx, String key) {
        return getSp(ctx).getString(key, "");
    }

    public static SharedPreferences getSp(Context ctx) {
        return ctx.getSharedPreferences(SP_NAME, Context.MODE_MULTI_PROCESS);
    }

    public static String getBindKey(Context ctx) {
        return getSp(ctx).getString(EXTRA_KEY, "");
    }

    static String getWeixinId(Context ctx) {
        return getSp(ctx).getString(EXTRA_WEIXIN, "");
    }

    static void setWeixinId(Context ctx, String id) {
        getSp(ctx).edit().putString(EXTRA_WEIXIN, id).commit();
    }

    static void setBind(Context ctx, boolean isBind) {
        getSp(ctx).edit().putBoolean(EXTRA_BIND_FLAG, isBind).commit();
    }

    static boolean getBind(Context ctx) {
        return getSp(ctx).getBoolean(EXTRA_BIND_FLAG, false);
    }

}
